<%
'' 常用函数
''''''''''''''''''''''''''''''''''''''''

''输出文本
sub echo(byval str)
	response.write str
end sub
''响应结束
sub died()
	response.end()
end sub

''''''''' 读取语言 ''''''''
function eu_lang(byval opt)
	eu_lang = eucms.lang.get(opt)
end function

''配置
function eu_config(byval opt)
	eu_config = eucms.conf.get(opt)
end function

''找到语言配置文件路径
'' {lang:common.index}   {lang:model.article.title}
function eu_lang_file(byval opt, byval lng)
	dim sp : sp = split(opt,".")
	eu_lang_file = eu_cfgpth(sp(0)) & "/language/"& eucms_lang &".lng"
	if not eu_isnull(lng) then eu_lang_file = eu_cfgpth(sp(0)) & "/language/"& lng &".lng"
end function

''找到配置文件的路径 common 不需要
function eu_config_file(byval opt,byval file)
	dim sp : sp = split(opt,".")
	eu_config_file = eu_cfgpth(sp(0)) & "/config/config.cfg
	if not eu_isnull(file) then eu_config_file = eu_cfgpth(sp(0)) &"/config/"& file &".cfg"
end function

function eu_cfgpth(byval opt)
	dim sp : sp = split(opt,".")
	dim pth
	select case lcase(trim(sp(0)))
		case "common",""
			pth = eucms_path & "common"
		case "model"
			pth = eucms_path & "model" & trim(sp(1))
		case "admin" '仅能在当前目录下
			pth = "./"
		case "plugin"
			pth = eucms_path & "plugin" & trim(sp(1))
		case "user"
			pth = eucms_path & "user"
		case "bbs"
			pth = eucms_path & "bbs"
	end select
	eu_cfgpth = pth
end function

'''''''''''''''' 缓存操作 '''''''''''''''

''写Cookies
function eu_setcookies(byval name, byval val, byval expire)
	Response.Cookies(eucms_cachpre&"cookies")( eu_strfilter(name) ) = val
	Response.Cookies(eucms_cachpre&"cookies").Expires = Date() + expire
end function

''读Cookies
function eu_getcookies(byval name)
	eu_getcookies = Request.Cookies(eucms_cachpre&"cookies")( eu_strfilter(name) )
end function

''写application
function eu_setcache(byval key, byval val)
	n = eu_cachepre & lcase(eu_strfilter(key))
	dim da:da = application(n)
	if isarray(da) then
		da(0) = val
		da(1) = now()
	else
		Redim da(2)
		da(0) = val
		da(1) = now()
	end if
	application.lock
	application( n ) = da
	application.unlock
end function

''读application
function eu_getcache(byval key)
	dim da
	n = eu_cachepre & lcase(eu_strfilter(key))
	da = application(n)
	if isarray(da) then eu_getcache = da(0) else eu_getcache = ""
end function

'清空application
function eu_removecache()
	application.lock
	for each o in application.contents
		if eu_strleft(o, eu_strlen(eucms_cachepre)) = cstr(eucms_cachepre) then application.contents.Remove(o)
	next
	application.unlock
end function

'''''''''''''''' 组合html标签 ''''''''''''''''

''表单开始
function eu_openForm(byval action, byval caption, byval onsubmit)
	dim s1,c1
	c1="eucms_form_list"
	s1 = "<form name='"&c1&"' id='"&c1&"' method='post' action='"&action&"' onsubmit='return "&onsubmit&"'>"
	s1 = s1 &"<table class='eu_form_tb' cellspacing='0'>"
	if not eu_isnull(caption) then s1=s1&"<caption>"&caption&"</caption>"
	eu_openForm = s1
end function

''表单结束
function eu_closeForm()
	dim s1
	s1 = "</table>"
	s1 = s1 &"<div class='eu_submit'>"
	s1 = s1 &"<input type='submit' name='submit' value='"&eu_lang("submit")&"(<u>S</u>)' accesskey='s'> "
	s1 = s1 &"<input type='reset' name='reset' value='"&eu_lang("reset")&"(<u>R</u>)' accesskey='r'>"
	s1=s1&"</form>"
	eu_closeForm =  s1
end function

''插入到表单中
function eu_insertTd(byval key, byval value, byval help)
	dim s1
	s1 = "<tr><th>"& eu_lang(key) &"</th>"
	s1 = s1&"<td><table cellspacing='0'><tr><td>"&value&"</td>"
	if not eu_isnull(help) then s1=s1&"<td><img class='eu_help_img' src='' width=16 height=16 rel="""&help&"""></td>"
	s1 = s1 &"</tr></table></td></tr>"
	eu_insertTd = s1
end function


'''''''''''''''' 网络相关 ''''''''''''''''

''取客户端IP地址
function eu_ip
	eu_ip=request.servervariables("http_x_forwarded_for")
	if eu_ip="" then eu_ip=request.servervariables("remote_addr")
end function

''取服务器主机名
function eu_hostname
	eu_hostname=request.servervariables("server_name")
	if left(eu_hostname,4)="www." then	eu_hostname=right(server_name,len(server_name)-4)
end function

''下载远程图片(文件)
function eu_downfile(byval src, byval path)

	eu_downfile = true
end function

' 禁止外部提交
function eu_checkpost()
	m = cstr(request.servervariables("http_referer"))
	n = cstr(request.servervariables("server_name"))
	if Mid(m, 8, len(n)) <> n then
		call eu_error() : died()
	end if
end function

''get
function eu_get(byval key)
	v = Request.Querystring(key)
	if eu_isnull(v) then v = Request.Form(key)
	v = trim(v)
end function

''post
function eu_post(byval key)
	v = Request.Form(key)
	if eu_isnull(v) then v = eu_get(key)
end function

'''''''''''''''' 字符串操作 ''''''''''''''''

''清除掉一些特殊字符
function eu_strfilter(byval str)
	str = replace(str," ","") : str = replace(str,"'","") : str = replace(str,"=","")
	str = replace(str,"*","") : str = replace(str,"""","") : str = replace(str,"&","")
	eu_strfilter = str
end function

''模拟php中的htmlspecialchars
function htmlspecialchars(byval str)
	str = replace(str,"&", "&amp;") : str = replace(str,"""", "&quot;")
	str = replace(str,"<", "&lt;") : str = replace(str,">", "&gt;")
	htmlspecialchars = str
end function

''判断是否为空
function eu_isnull(byval str)
	str = trim(str)
	eu_isnull=false
	if len(str)=0 Or isNull(str) then	eu_isnull=true
end function

''字符串长度
function eu_strlen(byval str)
	dim i, strAsc
	eu_strlen = 0
	for i = 1 to len(str)
		strAsc = Abs(Asc(Mid(str, i, 1)))
		if strAsc > 255 Or strAsc<2 then
			eu_strlen = eu_strlen + 2
		else
			eu_strlen = eu_strlen + 1
		end if
	next
end function

''取左字符串
function eu_strleft(byval str, byval num)
	eu_strleft = eu_strmid(str,1,num)
end function

''取中间
function eu_strmid(byval str, byval start, byval num)
	dim i,j,c,s
	s=trim(str) : j=0
	for i=1 to len(s)
		c = Abs(Asc(Mid(s, i, 1)))
		if c>255 or c<2 then j=j+2 else j=j+1
		if j>=num then mid_str = Mid(s, start, i) : exit function
	next
	eu_strmid = Mid(s, start, i)
end function

''替换字符(正则)
function eu_replace(byval html,byval patterns,byval replaceval)
	dim reg : set reg = new regexp : reg.ignorecase = true : reg.global = true
	reg.pattern = patterns
	dim newval : newval = replaceval
	eu_replace = reg.replace(html, newval)
end function

''检查合法性
function eu_strchk(byval chkstr,byval chktype)
	dim pattern
	select case cstr(chktype)
	case "1": pattern = "^\d+$"   ' 数字
	case "2": pattern = "^[A-Za-z]+$"   ' 英文
	case "3": pattern = "^[a-zA-Z0-9\,\/\-\_\[\]]+$"   ' 英文/数字及字符[,/-_]
	case "4": pattern = "^[A-Za-z0-9\_\-]+$"   ' 英文/数字级字符-_
	case "5": pattern = "^[A-Za-z0-9]+$"   ' 由数字和26个英文字母组成的字符串
	case "6": pattern = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"   ' Email地址
	case "7": pattern = "^(http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+"   ' http,https,ftp地址
	case "8": pattern = "^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"   ' 日期时间验证
	case "9": pattern = "^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"   ' IP地址验证
	case "10": pattern = "^((http|https|ftp):(\/\/|\\\\)(([\w\/\\\+\-~`@:%])+\.)+([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+|\/([\w\/\\\.\=\?\+\-~`@\':!%#]|(&)|&)+)\.(jpeg|jpg|gif|png|bmp)$"  ' http,https,ftp开头的图片路径,不支持中文
	case "11": pattern = "^\w+\.(\w){1,30}$"  ' 文件名格式,不支持中文
	case "12": pattern = "^[0-9\,\.]+$"  ' 整数,带格式化,如:100,000
	case else: pattern = "^[A-Za-z0-9]+$"
	end select
	dim reg : set reg = new regexp : reg.ignorecase = true : reg.global = true
	reg.pattern = pattern
	eu_strchk = reg.test(trim(chkstr))
end function

''双零
function eu_double(byval str)
	str = trim(str)
	str = right("00"& str, 2)
	eu_double = str
end function

''简单html编码
function eu_htmlcode(byval l1)
	dim l2
	on error resume next
	if len(l1)>0 then
		l2=replace(l1,">","&gt;")
		l2=replace(l2,"<","&lt;")
		l2=replace(l2,chr(32)&chr(32)," &nbsp;")
		l2=replace(l2,chr(9),"&nbsp;")
		l2=replace(l2,chr(34),"&quot;")
		l2=replace(l2,chr(39),"&#39;")
		l2=replace(l2,chr(13),"")
		l2=replace(l2,chr(10),"<br />")
		eu_htmlcode=l2
	end if
end function

''三目
function iif(byval A,byval B,byval C)
	if A then iif = B else iif = C
end function

'''''''''''''''' 日期操作 ''''''''''''''''

''取当前日期时间
function eu_now()
	eu_now = year(date) & "-" & month(date) & "-" & day(date) & " " & hour(time) & ":" & minute(time) & ":" & second(time)
end function

''取日期
public function eu_getdate(byval i1)
	i1 = trim(i1)
	if Not isDate(i1) then i1 = eu_now()
	eu_getdate = i1
end function

''格式化日期
function eu_formatdate(byval l1,byval l2)
	dim I1
	l1 = eu_getdate(l1)
	if len(l1)>0 then
		select case cstr(l2)
		case"0"
			I1=year(l1)&"-"&month(l1)&"-"&day(l1)&" "&hour(l1)&":"&minute(l1)&":"&second(l1)
		case"1"
			I1=year(l1)&"-"&eu_double(month(l1))&"-"&eu_double(day(l1))&" "&eu_double(hour(l1))&":"&eu_double(minute(l1))&":"&eu_double(second(l1))
		case else
			I1=replace(l2,"yyyy",year(l1))
			I1=replace(I1,"yy",right(year(l1),2))
			I1=replace(I1,"MM",eu_double(month(l1)))
			I1=replace(I1,"dd",eu_double(day(l1)))
			I1=replace(I1,"hh",eu_double(hour(l1)))
			I1=replace(I1,"mm",eu_double(minute(l1)))
			I1=replace(I1,"ss",eu_double(second(l1)))
			I1=replace(I1,"M",month(l1))
			I1=replace(I1,"d",day(l1))
			I1=replace(I1,"h",hour(l1))
			I1=replace(I1,"m",minute(l1))
			I1=replace(I1,"s",second(l1))
		end select
		eu_formatdate=I1
	end if
end function

''比较日期 i1 间隔类型  i2老日期  i3现在的日期
function eu_datediff(byval i1, byval i2, byval i3)
	if eu_isnull(i2) or eu_isnull(i3) then eu_datediff=0
	eu_datediff = datediff(i1, i2, i3)
end function

''***** 对象操作 *************************

''判断是否能创建对象
function eu_iscreateobj(byval l1)
	dim l2
	on error resume next
	eu_iscreateobj=false
  set l2=server.CreateObject(byval l1)
	if err.number=0 then eu_iscreateobj=true
  set l2=nothing
	if err.number<>0 then err.clear
end function

''创建对象
function eu_createobj(byval i1)
	on error resume next
	set eu_createobj = Server.CreateObject( i1 )
	if err then
		err.clear : echo "无法创建对象: "& i1
		died
	end if
end function

''是否为对象
function eu_isobj(byval i1)
	eu_isobj = false
	if isObject( i1 ) then eu_isobj = true
end function

''创建FSO对象
function eu_createfso()
	on error resume next
	set eu_createfso = eu_createobj( eucms_fsoname )
	if err then err.clear : echo("无法创建FSO对象!") : died()
end function


'''''''''''''''' 文件目录操作 ''''''''''''''''
''创建目录
function eu_createfolder(byval dirpath)
	on error resume next
	Dim fso : set fso = eu_createfso()
	dirpath=trim(dirpath)
	dirpath = replace(dirpath, "\", "/") : dirpath = replace(dirpath, "//", "/")
	dim subpath, pathdeep, i
	dirpath = replace(server.mappath(dirpath), server.mappath("/"), "")
	if right(dirpath,1)<>"\" then dirpath=dirpath&"\" :	subpath = split(dirpath, "\")
	pathdeep = pathdeep & server.mappath("/")
	on error resume next
	for i = 1 to ubound(subpath)-1
		pathdeep = pathdeep & "/" & subpath(i) ': response.write pathdeep
		if not fso.folderexists(pathdeep) then fso.createfolder pathdeep
	next
	if err then err.clear: eu_createfolder = false else eu_createfolder = true
	set fso=nothing
end function

''删除目录
function eu_deletefolder(byval dirpath)
	on error resume next
	Dim fso : set fso = eu_createfso()
	fso.deletefolder server.mappath(dirpath)
	if err then err.clear: eu_deletefolder = false else eu_deletefolder = true
	set fso=nothing
end function

''拷贝目录
function eu_copyfolder(byval l1, byval l2)
	on error resume next
	Dim fso : set fso = eu_createfso()
	fso.copyfolder server.mappath(l1),server.mappath(l2)
	if err.number<>0 then err.clear : eu_copyfolder=false else eu_copyfolder=true
	set fso=nothing
end function

''读取文本
function eu_readfile(byval filename)
	dim obj
	on error resume next
	set obj = server.createobject( eucms_streamname )
	obj.Type = 2
	obj.Mode = 3
	obj.open
	obj.Charset = response.charset
	obj.Position = obj.Size
	obj.Loadfromfile server.mappath(filename)
	eu_readfile = obj.ReadText
	obj.close
	set obj = nothing
end function

''保存文本
function eu_savefile(byval content,byval filedir)
	filedir = replace(filedir, "\", "/") : filedir = replace(filedir, "//", "/")
	if right(filedir, 1) = "/" then filedir = filedir & "index." & eucms_htmlext
	call create_folder(filedir)
	on error resume next
	dim obj : set obj = server.createobject( eucms_streamname )
	obj.type = 2
	obj.open
	obj.charset = response.charset
	obj.position = obj.Size
	obj.writeText = content
	obj.savetofile server.mappath(filedir), 2
	obj.close
	if err then err.clear:eu_savefile = false else eu_savefile = true
	set obj = nothing
end function

''文件是否存在
function eu_fileexists(byval i1)
	on error resume next
	dim i2
	Dim fso : set fso = eu_createfso()
	i2 = server.MapPath( i1 )
	eu_fileexists = fso.fileexists(i2)
	if err then err.clear
	set fso=nothing
end function

''删除文件
function eu_deletefile(byval filedir)
	filedir = replace(filedir, "\", "/") : filedir = replace(filedir, "//", "/")
	if right(filedir, 1) = "/" then
		eu_deletefile = eu_deletefolder(filedir)
	else
		on error resume next
		Dim fso : set fso = eu_createfso()
		fso.deletefile server.mappath(filedir) : set fso=nothing
		if err then err.clear : eu_deletefile = false else eu_deletefile = true
	end if
end function

''拷贝文件
function eu_copyfile(byval l1, byval l2)
	on error resume next
	Dim fso : set fso = eu_createfso()
	eu_copyfile=true
	fso.copyfile server.mappath(l1),server.mappath(l2) : set fso=nothing
	if err.number<>0 then err.clear : eu_copyfile=false
end function

''移动文件
function eu_movefile(byval l1, byval l2)
	on error resume next
	Dim fso : set fso = eu_createfso()
	eu_movefile=true
	fso.movefile server.mappath(l1), server.mappath(l2) : set fso=nothing
	if err.number<>0 then err.clear : eu_movefile=false
end function

''格式化文件大小
function eu_formatsize(ByVal tsize)
  dim csize
  If tsize >= 1073741824 Then
    csize = Int((tsize / 1073741824) * 1000) / 1000 & " GB"
  ElseIf tsize >= 1048576 Then
    csize = Int((tsize / 1048576) * 1000) / 1000 & " MB"
  ElseIf tsize >= 1024 Then
    csize = Int((tsize / 1024) * 1000) / 1000 & " KB"
  Else
    csize = tsize & "B"
  End If
  eu_formatsize = csize
end function


%>